const express = require('express')
const bodyParser = require('body-parser')
const multiparty = require('multiparty')
const fse = require('fs-extra')
const path = require('path');
const fs = require('fs');

const app = express();

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

const UPLOAD_DIR = path.resolve(__dirname,'public/upload');
app.post('/upload',function(req,res){
    const form = new multiparty.Form({uploadDir:'temp'})
    form.parse(req);
    form.on('file',async (name,chunk) => {
        //存放切片的目录
        let chunkDir = `${UPLOAD_DIR}/${chunk.originalFilename.split('.')[0]}`
        if(!fse.existsSync(chunkDir)){
            await fse.mkdirs(chunkDir);
        }
        //分片按索引编号，再次命名,原文件名.index.ext
        let dPath = path.join(chunkDir,chunk.originalFilename.split('.')[1]);
        //把分片移动到同名的存放目录
        await fse.move(chunk.path,dPath,{overwrite:true});
    })
    res.send('文件上传成功');
})

app.post('/merge',async function(req,res){
    let name = req.body.name;
    let fname = name.split('.')[0];

    //获取文件名的所有分片
    let chunkDir = path.join(UPLOAD_DIR,fname);
    let chunks = await fse.readdir(chunkDir);

    //分片按索引进行排序
    chunks.sort((a,b)=>a-b).map(chunkPath => {
        //合并文件
        fs.appendFileSync(
            path.join(UPLOAD_DIR,name),
            fs.readFileSync(`${chunkDir}/${chunkPath}`)
        )
    })
    //合并完成后删除分片目录
    fse.removeSync(chunkDir);
    //然后返回成功合并后的url地址
    res.send({
        msg:'合并成功',
        url:`http://localhost:3000/upload/${name}`
    })
})
app.listen(3000);
console.log('listen 3000');